package com.smarthospital.dao.im.daoService.impl;

import com.smarthospital.dao.im.dao.IMMessageDao;
import com.smarthospital.dao.im.daoService.IMMessageDaoService;
import com.smarthospital.entity.im.IMMessage;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

/**
 * create by sym  2021/5/9 21:20.
 */
@Slf4j
@Service
public class IMMessageDaoServiceImpl implements IMMessageDaoService {
    @Autowired
    private IMMessageDao imMessageDao;

    @Autowired
    private ElasticsearchRestTemplate template;



    @Override
    public Page<IMMessage> getByOrderId(String orderId) {
        //排序构建
        TermQueryBuilder query = QueryBuilders.termQuery("orderId", orderId);
        PageRequest request = PageRequest.of(0, 1000, Sort.by(Sort.Direction.DESC, "timeStamp"));
        Page<IMMessage> search = imMessageDao.search(query, request);
        return search;
    }

    @Override
    public void save(IMMessage imMessage) {
        imMessageDao.save(imMessage);
    }


}
